#!/usr/bin/env python3
from sys import argv
import random as rdm
import math

_g_fn_menu_meat = "meat.txt"
_g_fn_menu_veg = "veg.txt"

# ============================================================================
def _get_nmv(num_person):
    """ get num of (meat,veg) """
    num_dishes = num_person + 1
    num_veg = int(num_dishes * 0.33)
    num_meat = num_dishes - num_veg
    return (num_meat, num_veg)

# ============================================================================
def _is_valid_line(line):
    """check if this line is valid"""
    if len(line) == 0:
        return False

    ans = False
    _comment_leaders = ["#", " ", "!", "/"]
    if line[0] not in _comment_leaders:
        ans = True
        pass

    return ans

# ============================================================================
def _get_list_dishes(_which):
    """
    read menu, return list of dishes
    _which = "veg" or "meat"
    """

    list_dishes = []
    fn = ""
    if _which == "veg":
        fn = _g_fn_menu_veg
    else:
        fn = _g_fn_menu_meat
        pass

    # read here
    with open(fn, "r") as f:
        while(1):
            line = f.readline()
            if not line:  # Check EOF
                break
            line = line.replace("\n", "")  # remove newline
            if _is_valid_line(line):
                list_dishes.append(line)
            pass
        pass

    return list_dishes



# ============================================================================
def generate_order(num_person):
    num_meat, num_veg = _get_nmv(num_person)
    list_full_meat = _get_list_dishes("meat")
    list_full_veg = _get_list_dishes("veg")

    order_meat = rdm.sample(list_full_meat, k=num_meat)
    order_veg = rdm.sample(list_full_veg, k=num_veg)
    order = order_meat + order_veg
    return order


# ============================================================================
def print_order(order):
    n_perline = 3
    n_line = int(math.ceil(len(order) / float(n_perline)))  # full_lines
    # n_residual = len(order) % n_perline
    for i in range(n_line):
        ibeg  = i * n_perline
        iend  = min((i + 1) * n_perline, len(order))
        words = order[ibeg: iend]
        n_words = len(words)
        fmt  = "{:<12} " * n_words + "\n"
        line = fmt.format(*words)
        print(line, end="")
        pass
    return None


# ============================================================================
def real_main():
    num_person = -1
    if len(argv) > 1:
        num_person = int(argv[1])
        pass
    else:
        print("please input a number as num person\n")
    order = generate_order(num_person)

    # print(order)
    print_order(order)

    return None


# ============================================================================
if __name__ == "__main__":
    real_main()


